package com.iwhalecloud.citybrain.flow.platfrom.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.iwhalecloud.citybrain.flow.platform.client.FlowHttpClient;
import com.iwhalecloud.citybrain.flow.platform.client.util.GsonHelperUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpCommandResultDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpCompleteWorkItemDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpCreateProcessInstanceDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpSetParticipantsDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpStartProcessInstanceDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpTaskNoticeSettingDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpTaskRollbackDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpTerminateProcessInstanceDto;
import org.junit.After;
import org.junit.Before;

/**
 * @author lilh
 * @date 2019-01-22 10:56
 */
public class ClientTest {

    private FlowHttpClient client;

    @Before
    public void before() {
        this.client = new FlowHttpClient();
        //        client.setUrl("http://localhost:9999/flow-platform");
        //client.setUrl("http://localhost:9999/flow-platform");
        client.setUrl("http://flow-web.dev.dxt.com/flow-platform");
    }

    @After
    public void after() {
        this.client = null;
    }

    //@Test
    public void testGetModels() {
        System.out.println(GsonHelperUtils.toJson(client.getModels("WeChat11111")));
    }

    //@Test
    public void testCreateProcessInstance() {
        HttpCreateProcessInstanceDto createProcessInstanceDto = new HttpCreateProcessInstanceDto();
        //api发布
        createProcessInstanceDto.setFlowPackageCode("cswbdy001");
        createProcessInstanceDto.setParticipantId("18679796770");
        createProcessInstanceDto.setParticipantName("李良宏");
        createProcessInstanceDto.setParticipantType("USR");
        createProcessInstanceDto.setFrom("TEST");
        System.out.println(GsonHelperUtils.toJson(createProcessInstanceDto));
        HttpCommandResultDto resultDto = client.createProcessInstance(createProcessInstanceDto);
        System.out.println(GsonHelperUtils.toJson(resultDto));
    }

    //@Test
    public void testSetParticipants() {
        Map<String, Object> map = new HashMap<>();
        map.put("SERIAL", "1552899379730");
        map.put("PROCESSINSTANCEID", "867508");
        map.put("COMMAND_CODE", "setParticipants");
        map.put("TIME", "2019-03-18 16:56:19");
        map.put("PRIORITY", "1");
        map.put("FROM", "XJDA");
        map.put("TO", "FLOW");
        map.put("AREA_CODE", "1");
        map.put("TACHE_LIST", "{\"kl_002\":{\"SPLITTYPE\":\"OR\",\"PARTICIPANTS\":[{\"participantId\":\"320322197805114226\",\"participantType\":\"USR\",\"participantName\":\"彭骅石\"}]},\"kl_001\":{\"SPLITTYPE\":\"OR\",\"PARTICIPANTS\":[{\"participantId\":\"320322196612172396\",\"participantType\":\"USR\",\"participantName\":\"符南\"}]}}");
        HttpSetParticipantsDto setParticipantsDto = new HttpSetParticipantsDto();
        setParticipantsDto.init(map);
        System.out.println(GsonHelperUtils.toJson(setParticipantsDto));
        //HttpCommandResultDto resultDto = client.setParticipants(setParticipantsDto);
        //System.out.println(resultDto.toMap());
    }

    //@Test
    public void testStartProcessInstance() {
        HttpStartProcessInstanceDto startProcessInstanceDto = new HttpStartProcessInstanceDto();
        startProcessInstanceDto.setFrom("TEST");
        Map<String, Object> params = new HashMap<>();
        params.put("type", "1");
        startProcessInstanceDto.setFlowParamList(params);
        startProcessInstanceDto.setProcessInstanceId("c0d068b0869a11e9a0a34e9ee95a7f5c");
        System.out.println(GsonHelperUtils.toJson(startProcessInstanceDto));
        HttpCommandResultDto resultDto = client.startProcessInstance(startProcessInstanceDto);
        System.out.println(GsonHelperUtils.toJson(resultDto));
    }


    //@Test
    public void testCompleteWorkItem() {
        HttpCompleteWorkItemDto completeWorkItemDto = new HttpCompleteWorkItemDto();
        completeWorkItemDto.setFrom("TEST");
        completeWorkItemDto.setProcessInstanceId("c0d068b0869a11e9a0a34e9ee95a7f5c");
        completeWorkItemDto.setWorkItemId("546be27e869b11e9a0a34e9ee95a7f5c");
        Map<String, Object> params = new HashMap<>();
        params.put("approval", "pass");
        params.put("comment", "审批通过");
        completeWorkItemDto.setFlowParamList(params);
        completeWorkItemDto.setTime("2019-06-03 14:40:15");
        completeWorkItemDto.setParticipantId("18679796770");
        completeWorkItemDto.setParticipantName("李良宏");
        completeWorkItemDto.setParticipantType("USR");
        System.out.println(GsonHelperUtils.toJson(completeWorkItemDto));
        HttpCommandResultDto resultDto = client.completeWorkItem(completeWorkItemDto);
        System.out.println(GsonHelperUtils.toJson(resultDto));
    }

    //@Test
    public void testTerminateProcessInstance() {
        HttpTerminateProcessInstanceDto terminateProcessInstanceDto = new HttpTerminateProcessInstanceDto();
        terminateProcessInstanceDto.setFrom("TEST");
        terminateProcessInstanceDto.setProcessInstanceId("252ae25285d011e99d154e9ee95a7f5c");
        terminateProcessInstanceDto.setTime("2019-06-03 14:40:15");
        System.out.println(GsonHelperUtils.toJson(terminateProcessInstanceDto));
        HttpCommandResultDto httpCommandResultDto = client.terminateProcessInstance(terminateProcessInstanceDto);
        System.out.println(GsonHelperUtils.toJson(httpCommandResultDto));
    }

    //@Test
    //public void testTodoTask() {
    //    HttpTaskQueryDto taskQueryDto = new HttpTaskQueryDto();
    //    taskQueryDto.setAssignee("18679796770");
    //    System.out.println(GsonHelperUtils.toJson(taskQueryDto));
    //    List<HttpTaskQueryInfoDto> taskQueryInfoDtos = client.todoTask(taskQueryDto);
    //    System.out.println(GsonHelperUtils.toJson(taskQueryInfoDtos));
    //}

    //@Test
    //public void testHistoryTask() {
    //    HttpTaskQueryDto taskQueryDto = new HttpTaskQueryDto();
    //    taskQueryDto.setAssignee("18679796770");
    //    taskQueryDto.setTimeStart("2019-06-05 13:59:37");
    //    taskQueryDto.setTimeEnd("2019-06-26 13:59:37");
    //    System.out.println(GsonHelperUtils.toJson(taskQueryDto));
    //    List<HttpTaskQueryInfoDto> taskQueryInfoDtos = client.historyTask(taskQueryDto);
    //    System.out.println(GsonHelperUtils.toJson(taskQueryInfoDtos));
    //}


    //@Test
    public void testTaskRollback() {
        HttpTaskRollbackDto taskRollbackDto = new HttpTaskRollbackDto();
        taskRollbackDto.setParticipantId("18679796770");
        taskRollbackDto.setParticipantName("李良宏");
        taskRollbackDto.setParticipantType("USR");
        taskRollbackDto.setProcessInstanceId("5978f586a31911e9b6fe4e9ee95a7f5c");
        Set<String> targetActivityIds = new HashSet<>();
        targetActivityIds.add("renwu01");
        taskRollbackDto.setTargetActivities(targetActivityIds);
        taskRollbackDto.setComment("测试回退");
        System.out.println(GsonHelperUtils.toJson(taskRollbackDto));
    }

    //@Test
    public void testTaskNoticeSetting() {
        HttpTaskNoticeSettingDto httpTaskNoticeSettingDto = new HttpTaskNoticeSettingDto();
        httpTaskNoticeSettingDto.setParticipantId("18679796770");
        httpTaskNoticeSettingDto.setParticipantName("李良宏");
        httpTaskNoticeSettingDto.setParticipantType("USR");
        httpTaskNoticeSettingDto.setProcessInstanceId("5978f586a31911e9b6fe4e9ee95a7f5c");

        HttpTaskNoticeSettingDto.NodeItem nodeItem = new HttpTaskNoticeSettingDto.NodeItem();
        nodeItem.setNodeCode("renwu01");
        nodeItem.setNoticeItems(new ArrayList<>());

        //任务办理通知
        HttpTaskNoticeSettingDto.NoticeItem noticeItem = new HttpTaskNoticeSettingDto.NoticeItem();
        noticeItem.setNoticeType("TASK_HANDLE");
        noticeItem.setNotice("Y");
        noticeItem.setNoticeTitle("通知标题");
        noticeItem.setNoticeContent("通知内容");
        noticeItem.setNotifiers(new HashSet<String>() {
            private static final long serialVersionUID = -8760345697077462926L;

            {
                add("18679796770");
                add("15577903540");
            }
        });
        noticeItem.setNoticeChannels(new HashSet<String>() {

            private static final long serialVersionUID = -7909857275949637366L;

            {
                add("SYSTEM");
                add("DINGDING");
                add("SMS");
                add("MAIL");
                add("POLICE_SERVICE");
            }
        });

        nodeItem.getNoticeItems().add(noticeItem);

        //超时通知
        HttpTaskNoticeSettingDto.NoticeItem timeoutNoticeItem = new HttpTaskNoticeSettingDto.NoticeItem();
        timeoutNoticeItem.setNoticeType("TIMEOUT");
        timeoutNoticeItem.setNotice("Y");
        timeoutNoticeItem.setCalendarType("CALENDAR_DAY");
        timeoutNoticeItem.setDay(0);
        timeoutNoticeItem.setHour(1);
        timeoutNoticeItem.setMinutes(20);
        timeoutNoticeItem.setNotifiers(new HashSet<String>() {

            private static final long serialVersionUID = 5127238897619554202L;

            {
                add("18679796770");
                add("15577903540");
            }
        });
        timeoutNoticeItem.setNoticeChannels(new HashSet<String>() {

            private static final long serialVersionUID = 175002236902314782L;

            {
                add("SYSTEM");
                add("DINGDING");
                add("SMS");
                add("MAIL");
                add("POLICE_SERVICE");
            }
        });
        timeoutNoticeItem.setNoticeTitle("通知标题");
        timeoutNoticeItem.setNoticeContent("通知内容");
        nodeItem.getNoticeItems().add(timeoutNoticeItem);

        //预警通知
        HttpTaskNoticeSettingDto.NoticeItem warningNoticeItem = new HttpTaskNoticeSettingDto.NoticeItem();
        warningNoticeItem.setNoticeType("WARNING");
        warningNoticeItem.setNotice("Y");
        warningNoticeItem.setCalendarType("WORKING_DAY");
        warningNoticeItem.setWarningItems(new ArrayList<>());
        HttpTaskNoticeSettingDto.WarningItem warningItem1 = new HttpTaskNoticeSettingDto.WarningItem();
        warningItem1.setWarningCode("w001");
        warningItem1.setNotifiers(new HashSet<String>() {

            private static final long serialVersionUID = 4192072435209402549L;

            {
                add("18679796770");
                add("15577903540");
            }
        });
        warningItem1.setNoticeChannels(new HashSet<String>() {

            private static final long serialVersionUID = -2974348634547979600L;

            {
                add("SYSTEM");
                add("DINGDING");
                add("SMS");
                add("MAIL");
                add("POLICE_SERVICE");
            }
        });
        warningItem1.setDay(0);
        warningItem1.setHour(1);
        warningItem1.setMinutes(10);
        warningNoticeItem.getWarningItems().add(warningItem1);

        HttpTaskNoticeSettingDto.WarningItem warningItem2 = new HttpTaskNoticeSettingDto.WarningItem();
        warningItem2.setWarningCode("w002");
        warningItem2.setNotifiers(new HashSet<String>() {

            private static final long serialVersionUID = 4192072435209402549L;

            {
                add("18679796770");
                add("15577903540");
            }
        });
        warningItem2.setNoticeChannels(new HashSet<String>() {

            private static final long serialVersionUID = -2974348634547979600L;

            {
                add("SYSTEM");
                add("DINGDING");
                add("SMS");
                add("MAIL");
                add("POLICE_SERVICE");
            }
        });
        warningItem2.setDay(0);
        warningItem2.setHour(1);
        warningItem2.setMinutes(10);
        warningNoticeItem.getWarningItems().add(warningItem2);

        nodeItem.getNoticeItems().add(warningNoticeItem);

        httpTaskNoticeSettingDto.getNodeItems().add(nodeItem);

        System.out.println(GsonHelperUtils.toJson(httpTaskNoticeSettingDto));
    }
}
